package com.yhq.spring.order.config;

import com.yhq.spring.order.interceptor.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author hqyin
 * @date 2023/4/18 5:19 下午
 */
@Configuration
@Slf4j
public class InterceptorConfig{
    @Autowired
    private HandlerInterceptor[] interceptors;

    @Bean
    public WebMvcConfigurer createOrderAnnotationWebMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                for(HandlerInterceptor interceptor: interceptors) {
                    registry.addInterceptor(interceptor);
                }
            }
        };
    }

//    /**
//     * 使用InterceptorRegistry自带排序
//     * @return
//     */
//    @Bean
//    public WebMvcConfigurer createOrderAnnotationWebMvcConfigurer() {
//        return new WebMvcConfigurer() {
//            @Override
//            public void addInterceptors(InterceptorRegistry registry) {
//                registry.addInterceptor().order(1);
//            }
//        };
//    }
}
